package com.ys.service.storage.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.storage.TccWarehouse;
import com.ys.entity.storage.TccWarehouseArea;
import com.ys.mapper.storage.TccWarehouseAreaMapper;
import com.ys.service.storage.TccWarehouseAreaService;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 库区管理实现层
 */
@Service("tccWarehouseAreaService")
public class TccWarehouseAreaServiceImpl implements TccWarehouseAreaService {
    private static final Logger logger = LoggerFactory.getLogger(TccWarehouseAreaServiceImpl.class);


    @Autowired
    private TccWarehouseAreaMapper tccWarehouseAreaMapper;

    /**
     * 新增
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R add(Map<String, Object> params) throws Exception {
        String cck = params.get("cck").toString();
        String vkq = params.get("vkq").toString();
        if (cck == null || vkq == null) {
            return R.error("请补充必填项");
        } else if (new TccWarehouseArea().setVkq(vkq).setCck(cck).count() > 0 ) {
            return R.error("不符合库区名称-仓库名称-国家唯一");
        }
        TccWarehouseArea entity = JSON.parseObject(JSON.toJSONString(params), TccWarehouseArea.class);

        tccWarehouseAreaMapper.insert(entity);
        return R.ok(AddDto.closeAndReload());
    }
    @Override
    @Transactional
    public R edit(Map<String, Object> params) throws Exception {
        TccWarehouseArea entity = JSON.parseObject(JSON.toJSONString(params), TccWarehouseArea.class);
        tccWarehouseAreaMapper.update(entity);
        return R.ok("操作成功");
    }
}
